package com.alter.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.alter.model.EnabledEntity;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;

/**
 * <p>
 * 系统导航<br>
 * CODE树形节点路径, Priority 是当前节点的一个优先级， 和code 最后部分是一致的。
 * </p>
 * 
 * <pre>
 *   a 
 *   |-- a1   1
 *   |-- a2   2
 * </pre>
 * 
 * @author a_alter
 */
@Entity
@Table(name = "SE_NAVBAR")
@JsonIgnoreProperties(value = { "hibernateLazyInitializer" })
public class SeNavBar extends EnabledEntity {

    private static final long serialVersionUID = 1L;

    @Column(nullable = false, length = 50, unique = true)
    private String name;

    @Column(name = "colDesc", length = 200)
    private String desc;

    @Column(nullable = false, length = 150, name = "url")
    private String href;

    @Column(nullable = false, columnDefinition = "int default 1")
    private Integer level;

    @Column(nullable = false, columnDefinition = "int default -1")
    private Integer priority;

    /**
     * 不太好维护, 只是提供给mysql 这样不支持递归查询的使用。 在 oracle 或者 sqlserver
     * 中可以忽略该字段的设计,所以该字段不支持更新。
     */
    // @Column(nullable = false, length = 90, columnDefinition =
    // "varchar(90) default '0'", updatable=false)
    // private String code;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "pid", nullable = true)
    @JsonBackReference
    private SeNavBar parentBar;

    /**
     * 设置eager, 用于解决 json 解析时输出该字段，会引起no session 错误。 等待jackson mapping 修改完之后取消。
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parentBar")
    @JsonManagedReference
    private List<SeNavBar> childBar;

    /**
     * 该导航下文章列表
     */
    @OneToMany(mappedBy = "bar")
    @JsonIgnore
    private List<AfArticle> listArticle;

    public SeNavBar() {
    }

    public SeNavBar(String name) {
	this.name = name;
    }

    public SeNavBar(String name, String url) {
	this.name = name;
	this.href = url;
    }

    public SeNavBar(String name, String url, String desc) {
	this.name = name;
	this.href = url;
	this.desc = desc;
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public String getDesc() {
	return desc;
    }

    public void setDesc(String desc) {
	this.desc = desc;
    }

    public String getHref() {
	return href;
    }

    public void setHref(String href) {
	this.href = href;
    }

    public SeNavBar getParentBar() {
	return parentBar;
    }

    public void setParentBar(SeNavBar parentBar) {
	this.parentBar = parentBar;
    }

    public List<SeNavBar> getChildBar() {
	return childBar;
    }

    public void setChildBar(List<SeNavBar> childBar) {
	this.childBar = childBar;
    }

    public Integer getLevel() {
	return level;
    }

    public void setLevel(Integer level) {
	this.level = level;
    }

    public Integer getPriority() {
	return priority;
    }

    public void setPriority(Integer priority) {
	this.priority = priority;
    }

    public List<AfArticle> getListArticle() {
	return listArticle;
    }

    public void setListArticle(List<AfArticle> listArticle) {
	this.listArticle = listArticle;
    }

    // public String getCode() {
    // return code;
    // }
    //
    // public void setCode(String code) {
    // this.code = code;
    // }

}
